import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'package:ball_lion/entity/match/zq_odds_entity.dart';
import 'package:ball_lion/module/match/detail/lq_match_detail_logic.dart';
import 'package:ball_lion/module/match/detail/zq_match_detail_logic.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/server/api/soccer_api.dart';
import 'package:ball_lion/server/api/basketball_api.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/widgets/base/wqs_text.dart';

class LqOddsLogic extends MatchOddsLogic {
  final detail = Get.find<LqMatchDetailLogic>(tag: '${Get.arguments}');
  List<String> typeList = ["胜负", "让分", "大小分"];
  final List<int> _numList = [2001, 2002, 2003];
  List<List<String>> headTypeList = [
    ["客", "主"],
    ["客", "让", "主"],
    ["大", "盘", "小"]
  ];
  int _typeIndex = 0;
  late TabController tabController =
      TabController(length: typeList.length, vsync: this);
  List<List<ZqOddsEntity>?> _data = [null, null, null];
  List<List<ZqOddsEntity>?> get data => _data;
  int get typeIndex => _typeIndex;

  set typeIndex(int value) {
    _typeIndex = value;
    tabController.animateTo(_typeIndex);
    update();
  }

  @override
  void onReady() {
    requestData();
  }

  Future requestData() async {
    for (var i = 0; i < _numList.length; i++) {
      _data[i] =
          await BasketballApi.getLqMatchOdds(_numList[i], detail.matchId);
    }
    update();
  }
}

class ZqMatchOddsLogic extends MatchOddsLogic {
  final detail = Get.find<ZqMatchDetailLogic>(tag: '${Get.arguments}');
  List<String> typeList = ["胜平负", "让球", "让球胜平负", "进球数"];
  final List<int> _numList = [1001, 1006, 1002, 1007];
  List<List<String>> headTypeList = [
    ["胜", "平", "负"],
    ["主", "盘", "客"],
    ["胜", "平", "负"],
    ["大", "盘", "小"]
  ];
  int _typeIndex = 0;
  late TabController tabController =
      TabController(length: typeList.length, vsync: this);
  List<List<ZqOddsEntity>?> _data = [null, null, null, null];
  List<List<ZqOddsEntity>?> get data => _data;

  int get typeIndex => _typeIndex;

  set typeIndex(int value) {
    _typeIndex = value;
    tabController.animateTo(_typeIndex);
    update();
  }

  @override
  void onReady() {
    requestData();
  }

  Future requestData() async {
    for (var i = 0; i < _numList.length; i++) {
      _data[i] = await SoccerApi.getZqMatchOdds(_numList[i], detail.matchId);
    }
    update();
  }
}

class MatchOddsLogic extends GetxController with GetTickerProviderStateMixin {
  List<Widget> formSingleLine(List<OddsData> dataList) {
    TextStyle style = const TextStyle(fontSize: 12, color: Colours.text);
    List<Widget> list = [Container(), Container(), Container(), Container()];
    List<String> karry = ["", "", ""];
    for (var element in dataList) {
      switch (element.i) {
        case "d":
        case "s":
        case "z":
          list.replaceRange(0, 1, [formValue(element.o, element.st)]);
          karry[0] = remain2(element.kl);
          break;
        case "x":
        case "k":
        case "f":
          list.replaceRange(2, 3, [formValue(element.o, element.st)]);
          karry[2] = element.kl.noValue ? "" : "/${remain2(element.kl)}";
          break;
        case "p":
          list.replaceRange(1, 2, [formValue(element.o, element.st)]);
          karry[1] = element.kl.noValue ? "" : "/${remain2(element.kl)}";
          break;
      }
    }
    list.replaceRange(3, 4, [QsText.style(karry.join(), style: style)]);
    return list;
  }

  String remain2(String? data) {
    return data.noValue ? "" : double.parse(data!).toStringAsFixed(2);
  }

  Widget formValue(data, status) {
    String text = "";
    Widget? asset;
    Color color = Colours.text;
    if (status == 0) {
      text = remain2(data);
    } else if (status == 1) {
      text = remain2(data);
      color = Colours.redF13333;
      asset = Image.asset(height: 9, Utils.getImgPath("up_arrow_red.png"));
    } else if (status == 2) {
      text = remain2(data);
      color = Colours.green;
      asset = Image.asset(height: 9, Utils.getImgPath("down_arrow_green.png"));
    } else {
      text = remain2(data);
    }
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        QsText(text, fontSize: 12, color: color),
        if (asset != null) asset
      ],
    );
  }
}
